<?php

/* Affiche la liste des messages du membre */

require('config.php');
$format = Annuaire::lookup(KEY_FORMAT);

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

// update all unread message to read all message
if(isset($_POST['submit'])) {
    modelReportingDiscussion::markAllAsRead($_SESSION['id_user']);
}

if(isset($_GET['title_debug']) && trim($_GET['title_debug'])!=''){
	$_SESSION['title_debug'] = $_GET['title_debug'];
	$title_debug = $_GET['title_debug'];
}
else {
	unset($_SESSION['title_debug']);
}

$_SESSION['new_message'] = modelUser::hasNewMessage($_SESSION['id_user']);

$page = new rootLayoutPage();
$page->setTranslatePage(__FILE__);
$page->setBaliseTitle('profil_message_balise');
$page->setDescription('profil_message_description');

$page->addStyleSheet('table_sorter.css');
$page->addJavaScript('jQuery.pagination.js');
$page->addStyleSheet('pagination.css');
$translate = tmxTranslate('profil-message.xml', REP_XLAT_TEMPLATE);
//-- menu Left show products--------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

//-- corps de la page ----------------------------//
$tpl_file='profil-message.tpl';
$profil_message = new flyLayout(REP_TPL . $tpl_file);
$profil_message->start();
$language_folder = $_SESSION['language_code']==language_default?'':'/'.$_SESSION['language_code'];
$profil_message->setVariable('language_folder', $language_folder);
//$tabs = new layoutTabs();
//$tabs->pushLink('Mon compte', '/profil.php');
//$tabs->pushLink('Mes messages', '/profil-message.php');
//$profil_message->includeLayout('tabs', $tabs);
$limit = 0;
	if(isset($_POST['page_id'])) {
	    $page_id = $_POST['page_id'];
	}
	else {
	    $page_id = 0;
	    $limit = page_id*NUMBER_PER_PAGE . ", " . NUMBER_PER_PAGE;
	}

$listDiscussion = modelUser::getListLimitDiscussion($_SESSION['id_user'],$title_debug, $limit);
foreach($listDiscussion as $key => $discussion) {

    if(modelReportingDiscussion::isUnread($discussion->id_discussion)) {
        $profil_message->setVariable('titre', $discussion->titre." ".$translate['nouveau']);
        $profil_message->setVariable('style_ligne', 'style="font-weight: bold;"');
	}
    else {
        $profil_message->setVariable('titre', $discussion->titre);
        $profil_message->setVariable('style_ligne', '');
	}

	$profil_message->setVariable('date_message', affiche($discussion->date_modification,'dateheure'));
    $profil_message->setVariable('id_discussion', $discussion->id_discussion);
    $profil_message->parseList('discussion');
}
/* include pagination**/

	$listDis = modelUser::getListDiscussion($_SESSION['id_user']);
	$arrayTpl = array();
	$arrayPagination = array();
	$arrayTpl[1]='pagination_search_top';
	$arrayTpl[2]='pagination_search_bottom';
	for($i=1;$i<=2;$i++){
		if (file_exists(REP_TPL . $_SESSION['rep_language'] . $arrayTpl[$i].'.tpl')){
		$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		else{$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		$arrayPagination[$i]->start();
		$arrayPagination[$i]->setVariable('rep_ajax', REP_AJAX.'reporting/');
		$arrayPagination[$i]->setVariable('rep_item_lister', 'list_search_profile_message.php?lang='.$_SESSION['language_code']);
		$i==1?$arrayPagination[$i]->setVariable('rep_item_counter', 'list-profil-message-count-item.php?lang='.$_SESSION['language_code']):'';
		$i==1?$arrayPagination[$i]->setVariable('page',  $id_page):'';
		$i==1?$arrayPagination[$i]->setVariable('item_per_page', ITEM_PER_PAGE_REAL_ESTATE):'';
		$i==1?$arrayPagination[$i]->setVariable('num_display_entries', NUM_DISPLAY_ENTRIES):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_search', DELAY_SEARCH):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_fifty_mili_second', DELAY_FIFTY_MILI_SECOND):'';
		$i==1?$arrayPagination[$i]->setVariable('default_text_search', $_SESSION['title_debug']):'';
		$i==1?$arrayPagination[$i]->setVariable('num_items', $listDis->size()):'';

		$arrayPagination[$i]->showBlock('pagination_search');
		$arrayPagination[$i]->stop();
	}
	$profil_message->includeFile('pagination_search_up', $arrayPagination[1]);
    $profil_message->includeFile('pagination_search_down', $arrayPagination[2]);

$profil_message->stop();
$page->setBody($profil_message);

$page->display();

?>